package counsel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import Product.ProductVo;
import salary.SalaryVo;
import common.ClampFactory;

public class CounselDAO {
	
	private SqlSession session;
	CounselDTO coun;
	
	public CounselDAO() {
		try {
			session = ClampFactory.getFactory().openSession();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public CounselDTO getCoun() {
		return coun;
	}
	public void setCoun(CounselDTO coun) {
		this.coun = coun;
	}

	//검색용 목록
	public ArrayList<CounselDTO> lists(int start, int end){
		ArrayList<CounselDTO> lists = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("cs", coun);
			map.put("start", start);
			map.put("end", end);
			putMap(map);
			
			lists = (ArrayList<CounselDTO>)session.selectList("counsel.lists",map);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return lists;
	}
	
	//총 행의 수 반환
	public int totalRow(){
		int row = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("cs", coun);
			putMap(map);
			row = (int)session.selectOne("counsel.totalRow",map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}
	
	//검색조건있을시 map에 저장
	public void putMap(Map<String, Object> map){
		
		if(coun.getFull() == null || !coun.getFull().equals("full")){
			map.put("full", "no");
		}else{
			map.put("full", "full");
		}
		
		if(coun.getSa_dpm() == null || coun.getSa_dpm().equals("0")) map.put("dpm", "0");
		else map.put("dpm", "dpm");
		
		if(coun.getSa_gd() == null || coun.getSa_gd().equals("0")) map.put("gd", "0");
		else map.put("gd", "gd");
		
		if(coun.getCs_sd() == null || coun.getCs_sd().equals("0")) map.put("sded", "0");
		else map.put("sded", "sded");
		
		if(coun.getCs_loan() == null || coun.getCs_loan().equals("0")) map.put("loan", "0");
		else map.put("loan", "loan");
		
		if(coun.getCt_ing() == null || coun.getCt_ing().equals("0")) map.put("ing", "0");
		else map.put("ing", "ing");
	}
	
	//상담 상세보기
	public CounselDTO view(){
		CounselDTO dto = null;
		try {
			dto = (CounselDTO)session.selectOne("counsel.view",coun);
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return dto;
	}
	
	//상담 등록
	public int insert(){
		int rs = 0;
		try {
			rs = session.insert("counsel.insert",coun);
			if(rs>0){
				session.commit();
			}
			else{
				session.rollback();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return rs;
	}
	
	//상담 수정
	public int update(){
		int rs = 0;
		try {
			rs = session.insert("counsel.update",coun);
			if(rs>0){
				session.commit();
			}
			else{
				session.rollback();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return rs;
	}
	
	//사원정보 검색
	public SalaryVo salSearch(SalaryVo vo){
		SalaryVo sal = null;
		try {
			sal = (SalaryVo)session.selectOne("counsel.salSearch",vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return sal;
	}
	
	//상품 정보 검색
	public ProductVo proSearch(ProductVo vo){
		ProductVo pro = null;
		try {
			pro = (ProductVo)session.selectOne("counsel.proSearch",vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		session.close();
		return pro;
	}
	
}
